import { Component, OnInit} from '@angular/core';
import { HttpRequest } from "../../../service/httpRequest";
import { PageModel } from "../../../model/page-model";
import { Router }  from '@angular/router';
import { flyIn } from '../../../animations/fly-in';

@Component({
  selector: 'app-customize',
  templateUrl: './customize.component.html',
  styleUrls: ['./customize.component.css'],
  animations: [
    flyIn
  ]
})
export class CustomizeComponent implements OnInit {


  asideFolded:boolean = false;
  data:any;
  pageModel = new PageModel();
  oo:any = {devicename:'',province:'',city:'',district:''};
  followIds:any = {};
  selectedIds:any = {};
  page:number = 1;
  constructor(private httpRequest:HttpRequest,private _router:Router) { }

  ngOnInit() {
    this.getData();
  }

  getData(){
    this.httpRequest.sendGet('devices/page',{
      'guanzhu': 1,
      'page': this.page,
      'size': this.pageModel.itemsPerPage,
      'province':this.oo.province,
      'city':this.oo.city,
      'area':this.oo.district,
      'device_name':this.oo.devicename
    }).subscribe(res => {
      res = this.transData(res);
      this.followIds = {};
      this.data = res.result.aaData;     
      this.pageModel.setPage({'iTotalRecords':res.result.iTotalRecords});
    },err => console.log(err));
  }

  cancelFollow(){
    let didstr="";
    let dids = Object.keys(this.followIds);
    for(let i=0;i<dids.length;i++){
      didstr += dids[i];
      if(dids.length!=(i+1)){
        didstr +=",";
      }
    }
    this.httpRequest.sendPost('devices/quguan',{
      'device_ids':didstr
    }).subscribe(res => {
      alert("取关成功！");
      this.followIds = {};
      this.getData();
    },err => console.log(err));
  }

  public items = [];
 
  fn(e,id,item){
    let checkbox = e.target;
    if(checkbox.checked){
      if(this.items.length == 5 ){
        checkbox.checked = !checkbox.checked;
        alert('最多选中5条');
        return;
      }
      this.followIds[id] = id;
      
     if( !this.selectedIds[id]){
       this.selectedIds[id] = id;
       this.items.push(item);      
       this.asideFolded = true;
     }
    }else{
      delete this.followIds[id];
      delete this.selectedIds[id];
      this.items.splice(this.items.indexOf(item),1); 
   
    }  
  }


  

  public deviceIDs=[]
  compare(){    
    for(let key in this.selectedIds){
      this.deviceIDs.push(key.replace('/','|'))
    }
    console.log(this.deviceIDs.join(','))
    window.open("#/access/comparedata/"+this.deviceIDs.join(','),'_blank' );    
  }

    


  transData(data){
    for (var i = 0; i < data.result.aaData.length; i++){

      switch (data.result.aaData[i].locate_mode){
        case '0':
          data.result.aaData[i].locate_mode = '输入方式';
          break;
        case '1':
          data.result.aaData[i].locate_mode = 'IP';
          break;
        case '2':
          data.result.aaData[i].locate_mode = 'GPIS';
          break;
        default:
          break;
      }

      switch (data.result.aaData[i].online){
        case '0' :
          data.result.aaData[i].online = '离线';
          break;
        case '1' :
          data.result.aaData[i].online = '在线';
          break;
        default:
          break;
      }
      data.result.aaData[i].did = data.result.aaData[i].id.replace("/","|");
    }

    return data;
  }

  removeItem(item){
    this.items.splice(this.items.indexOf(item),1);
    let el = document.getElementById(item.id);
    el['checked'] = false;
    delete this.selectedIds[item.id];
  }



}
